﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using APIShoppingSystem.Models.CommodityInfoViewModel;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class CommodityInfoController : ControllerBase
	{
		private ICommodityInfoBll _commodityInfoBll;
		private ICommodityTypeBll _commodityTypeBll;
		public CommodityInfoController(ICommodityInfoBll commodityInfoBll, ICommodityTypeBll commodityTypeBll)
		{
			_commodityInfoBll = commodityInfoBll;
			_commodityTypeBll = commodityTypeBll;
		}

		/// <summary>
		/// 商品类别下拉
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		public IActionResult CommodityTypeOption()
		{
			CustomActionResult res = new CustomActionResult();
			var commodityTypes = _commodityTypeBll.GetCommodityTypeOption();
			res.IsSuccess = true;
			res.Status = 1;
			res.Msg = "成功";
			res.Datas = commodityTypes;
			return new JsonResult(res);
		}


		/// <summary>
		/// 商品展示、分页、模糊查询
		/// </summary>
		/// <param name="page"></param>
		/// <param name="limit"></param>
		/// <param name="CommodityName"></param>
		/// <param name="CategoryId"></param>
		/// <returns></returns>
		[HttpGet]
		public IActionResult CommodityInfoPageList(int page, int limit, string CommodityName, string CategoryId)
		{
			int count;
			object commodityInfos = _commodityInfoBll.GetCommodityInfoListByPage(page, limit, out count, CommodityName, CategoryId);

			return new JsonResult(new
			{
				code = 0,
				msg = "成功",
				count = count,
				data = commodityInfos
			});
		}

		/// <summary>
		/// 修改商品信息
		/// </summary>
		/// <param name="commodityId"></param>
		/// <returns></returns>
		[HttpPost]
		public IActionResult UpdateCommodityInfoOption([FromForm] string commodityId)
		{
			CustomActionResult res = new CustomActionResult();
			var commodityTypes = _commodityTypeBll.GetCommodityTypeOption();
			var commodityInfo = _commodityInfoBll.GetEntity(commodityId);
			res.Status = 1;
			res.IsSuccess = true;
			res.Msg = "成功";
			res.Datas = new
			{
				commodityTypes = commodityTypes,
				commodityInfo = commodityInfo
			};
			return new JsonResult(res);
		}


		/// <summary>
		/// 上传商品图片
		/// </summary>
		/// <param name="file"></param>
		/// <returns></returns>
		[HttpPost]
		public IActionResult Upload([FromForm]IFormFile file)
		{
			CustomActionResult res = new CustomActionResult();
			if (file != null)
			{
				//获取文件拓展名
				string extensionName = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
				UserInfo userInfo = this.GetSessionUserInfo();
				if (userInfo == null)
				{
					res.Msg = "未获取到登录用户信息";
					return new JsonResult(res);
				}
				var stream = file.OpenReadStream();

				string msg;
				//获取上传的excel文件，提交到数据库中
				bool isSuccess = _commodityInfoBll.Upload(stream, extensionName, file.Length, userInfo.ID, file.FileName);
				if (isSuccess)
				{
					res.IsSuccess = true;
					res.Status = 1;
					res.Msg = "成功";
					return new JsonResult(res);
				}
			}

			return new JsonResult(res);
		}


		/// <summary>
		/// 添加商品
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public IActionResult AddCommodityInfo([FromForm]AddCommodityInfoViewModel model)
		{
			CustomActionResult res = new CustomActionResult();
			CommodityInfo commodityInfo = new CommodityInfo()
			{
				ID = Guid.NewGuid().ToString(),
				CommodityName = model.CommodityName,
				Price = model.Price,
				Num = 0,
				Detail = model.Detail,
				CreateTime = DateTime.Now,
				CategoryId = model.CategoryId,
				IsDelete = false,
				CommodityImg = model.CommodityImg,
			};
			bool isSuccess = _commodityInfoBll.Add(commodityInfo);
			if (isSuccess)
			{
				res.Status = 1;
				res.IsSuccess = true;
				res.Msg = "成功";
			}
			return new JsonResult(res);
		}


		/// <summary>
		/// 软删除多个商品
		/// </summary>
		/// <param name="CommodityIds"></param>
		/// <returns></returns>
		[HttpPost]
		public IActionResult SoftDeleteCommodityInfos([FromForm]List<string> CommodityIds)
		{
			CustomActionResult res = new CustomActionResult();
			if (CommodityIds == null || CommodityIds.Count == 0)
			{
				res.Msg = "请选择要删除的数据";
			}
			else
			{
				_commodityInfoBll.SoftDeleteEntities(CommodityIds);
				res.Status = 1;
				res.Msg = "成功";
				res.IsSuccess = true;
			}

			return new JsonResult(res);
		}

		/// <summary>
		/// 软删除商品
		/// </summary>
		/// <param name="CommodityId"></param>
		/// <returns></returns>
		[HttpPost]
		public IActionResult SoftDeleteCommodityInfo([FromForm] string CommodityId)
		{
			CustomActionResult res = new CustomActionResult();
			if (_commodityInfoBll.SoftDeleteEntity(CommodityId))
			{
				res.Status = 1;
				res.Msg = "成功";
				res.IsSuccess = true;

			}
			return new JsonResult(res);
		}



		/// <summary>
		/// 购买商品的方法
		/// </summary>
		/// <param name="CommodityId"></param>
		/// <param name="Num"></param>
		/// <returns></returns>
		[HttpPost]
		public IActionResult PlayCommodity([FromForm] string CommodityId,[FromForm] int Num)
		{
			CustomActionResult res = new CustomActionResult();

			string msg = "";
			if (Num < 0)
			{
				res.Msg = "数量错误";
				return new JsonResult(res);
			}
			UserInfo userInfo = this.GetSessionUserInfo();
			bool isSuccess = _commodityInfoBll.PlayCommodityOrder(CommodityId, Num, userInfo, out msg);
			if (isSuccess)
			{
				res.Status = 1;
				res.Msg = msg;
				res.IsSuccess = true;
			}
			else
			{
				res.Msg = msg;
			}
			return new JsonResult(res);

		}

	}
}
